<?php

/**
 * Description of ContentBook
 *
 * @author Honza
 */
class ContentBook implements IMainBody {
  
  private $bookID; //id of selected book
  
  public function __construct($bookID = NULL) {
    $this->bookID = $bookID;
    if ($bookID == NULL && isset($_GET['ida'])) {
      $this->bookID = $_GET['ida'];
    }
  }
  
  public function getContent() {
    $content = '<H2>Detail of book</H2>';

    if ($this->bookID) {
      $bookID = $this->bookID;
      
      // find all information about a book in db
      $cols = TBL_BOOKS . '.title as book_title, ' . TBL_LANGUAGES . '.name as language_title, isbn, pages, imprint, edition, description';
      $sql = 'select ' . $cols . ' from ' . TBL_BOOKS;
      $sql .= ' join ' . TBL_LANGUAGES . ' using (id_language)';
      $sql .= ' where id_book = ' . $bookID;
      $book = DB::sql($sql);


      $content .= '<span class="titleColumn">Title</span>' . $book[0]['book_title'] . BR;

      // select all book authors
      $sql = 'select concat(name, \' \', surname) as autor from ' . TBL_AUTHORS_BOOKS;
      $sql .= ' join ' . TBL_AUTHORS . ' using(id_author)';
      $sql .= ' where id_book = ' . $bookID;

      $authors = DB::sql($sql);
      $numOfAdded = 0;
      foreach ($authors as $author) {
        if ($numOfAdded == 0) {
          $content .= '<span class="titleColumn">Author/Authors</span>' . $author['autor'] . BR;
        } else {
          $content .= '<span class="titleColumn">&nbsp;</span>' . $author['autor'] . BR;
        }
        $numOfAdded++;
      }

      $content .= '<span class="titleColumn">Language</span>' . $book[0]['language_title'] . BR;
      $content .= '<span class="titleColumn">ISBN</span>' . $book[0]['isbn'] . BR;
      $content .= '<span class="titleColumn">Pages</span>' . $book[0]['pages'] . ' pages' . BR;
      $content .= '<span class="titleColumn">Imprints</span>' . $book[0]['imprint'] . BR;
      $content .= '<span class="titleColumn">Edition</span>' . $book[0]['edition'] . '.' . BR;

      // select all book genres
      $sql = 'select name from ' . TBL_GENRES_BOOKS;
      $sql .= ' join ' . TBL_GENRES . ' using(id_genre)';
      $sql .= ' where id_book = ' . $bookID;

      $genres = DB::sql($sql);
      $numOfAdded = 0;
      foreach ($genres as $genre) {
        if ($numOfAdded == 0) {
          $content .= '<span class="titleColumn">Genre/genres</span>' . $genre['name'] . BR;
        } else {
          $content .= '<span class="titleColumn">&nbsp;</span>' . $genre['autor'] . BR;
        }
        $numOfAdded++;
      }
      
      $content .= '<span class="titleColumn">Description</span>' . $book[0]['description'] . '' . BR;
    } else {
      $content .= '<p>The book doesn´t exists</p>';
    }
    
    return $content;
  }

}

?>
